package com.temp;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @Title: Main.java
 * @author fusz
 * @date 2011-12-19
 * @version
 */
public class Main {

	public static void main(String[] args) {
		int data[][] = { { 1, 2, 8, 9 }, { 2, 4, 9, 12 }, { 4, 7, 10, 13 },{ 6, 8, 11, 15 } };
		System.out.println(search(data,4,4,5));
		System.out.println(jump1(15));
		System.out.println(jump2(0));

		//类似sql中order by的功能，移动时，整行移动，不打乱整行顺序
		int array[][] = new int[][] {     
	                  { 12, 34, 68, 32, 9, 12, 545 },     
	                  { 34, 72, 82, 57, 56, 0, 213 },     
	                  { 12, 34, 68, 32, 21, 945, 23 },     
	                  { 91, 10, 3, 2354, 73, 34, 18 },    
	                  { 12, 83, 189, 26, 27, 98, 33 },     
	                  { 47, 23, 889, 24, 899, 23, 657 },     
	                  { 12, 34, 68, 343, 878, 235, 768 },     
	                  { 12, 34, 98, 56, 78, 12, 546 },     
	                  { 26, 78, 2365, 78, 34, 256, 873 } };    
	    sort(array, new int[] {1});//某一列进行排序   
	    print(array);
		sort(array, 9, 7);
		System.out.println("###########");
		print(array);
	}
	/**
	 * 查找key
	 * @param a
	 * @param M
	 * @param N
	 * @param key
	 * @return
	 */
	public static boolean search(int a[][], int M, int N, int key) {
		int row = 0, col = N - 1;
		while (row <= M - 1 && col >= 0) {
			if (key == a[row][col])
				return true;
			if (key > a[row][col])
				row++;
			if (key < a[row][col])
				col--;
		}
		return false;
	}
	/**
	 * 青蛙跳 1/2步
	 * @param n
	 * @return
	 */
	public static int jump1(int n){
		if(n == 1 || n == 2)
		return n;
		return jump1(n-1) + jump1(n-2);
	}
	
	/**
	 * 青蛙跳 n步
	 * @param n
	 * @return
	 */
	public static double jump2(int n) {
		if(n<1)
		return 0.0;
		return Math.pow(2,n-1);
	}
	/**
	 * order by 
	 * @param ob
	 * @param order
	 */
	public static void sort(int[][] ob, final int[] order) {
        Arrays.sort(ob, new Comparator<Object>() {
            public int compare(Object o1, Object o2) {    
                int[] one = (int[]) o1;
                int[] two = (int[]) o2;
                for (int i = 0; i < order.length; i++) {
                    int k = order[i];
                    if (one[k] > two[k]) {    
                        return 1;
                    } else if (one[k] < two[k]) {    
                        return -1;
                    } else {
                        continue;  //如果按一条件比较结果相等，就使用第二个条件进行比较。  
                    }
                }
                return 0;    
            }    
        });   
    }
	/**
	 * 排序(需优化)
	 * @param a
	 * @param row
	 * @param col
	 */
	  public static void sort(int[][] a,int row ,int col ){
	    	 int temp = 0;
	    	 for (int b = 0; b < row*col; b++) {
		            for (int j = 0; j < row; j++) {
		                for (int i = 0; i < col; i++) {
		                    if (i == col-1 && j < row-1) {
		                        if (a[j][i] > a[j + 1][0]) {
		                            temp = a[j][i];
		                            a[j][i] = a[j + 1][0];
		                            a[j + 1][0] = temp;
		                        }
		                    } else if (i < col-1) {
		                        if (a[j][i] > a[j][i + 1]) {
		                            temp = a[j][i];
		                            a[j][i] = a[j][i + 1];
		                            a[j][i + 1] = temp;
		                        }
		                    }
		                }
		            }
		        }
	    }
	  public static void print(int[][] array){
		  for (int i = 0; i < array.length; i++) {    
			    for (int j = 0; j < array[i].length; j++) {    
			        System.out.print(array[i][j]+"\t");    
			    }
			    System.out.println();    
			}
	  }
}